package br.ucb.cesbdao.janelas;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.swing.JRViewer;
import br.ucb.cesbdao.beans.Frequencia;
import br.ucb.cesbdao.enuns.EnumFormatos;
import br.ucb.cesbdao.enuns.EnumTitulo;
import br.ucb.cesbdao.enuns.EnumRelatorio;
import br.ucb.cesbdao.relatoriosDTO.TrabalhadorDTO;
import br.ucb.cesbdao.relatoriosDTO.AtendimentoAnoDTO;
import br.ucb.cesbdao.relatoriosDTO.AtendimentoDetalheDTO;
import br.ucb.cesbdao.relatoriosDTO.AtendimentoDTO;
import br.ucb.cesbdao.relatoriosDTO.EncaminhamentoDTO;
import br.ucb.cesbdao.relatoriosDTO.FrequenciaGrupoDTO;
import br.ucb.cesbdao.relatoriosDTO.TipoAtendedimentoDTO;
import br.ucb.cesbdao.servicos.Servicos;

public class JanelaRelatorio extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7802769944243180623L;
	private final JPanel contentPanel = new JPanel();
	private JRViewer viewer;
	private JScrollPane scrollPane;
	static Logger logger = Servicos.carregaLog(JanelaRelatorio.class);


	public JanelaRelatorio() {
		setTitle(EnumTitulo.TITULO_RELATORIO.getTitulo());
		setBounds(100, 100, 800, 700);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		scrollPane = new JScrollPane();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
						.addGap(0))
				);
		gl_contentPanel.setVerticalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
						.addGap(0))
				);
		contentPanel.setLayout(gl_contentPanel);
		scrollPane.setViewportView(viewer);
	}
	public void showRelatorioEncaminhamento(
			ArrayList<EncaminhamentoDTO> encaminhamentoRls, Map<String, Object> parametros){
		try{
			JasperReport jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ENCANMINHA_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(encaminhamentoRls));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
	}
	public void showRelatorioQuantAtendimento(ArrayList<AtendimentoDTO> atendimentoTOs,String ano){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ano",ano);
		try{
			JasperReport jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATENDIMENTO.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,map,new JRBeanCollectionDataSource(atendimentoTOs));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
	}
	public void showRelatorioQuantAtendimento(ArrayList<AtendimentoAnoDTO> atos,Map<String, Object> parametros) {
		JasperReport jasper = null;
		try{
			if(Janela.getTipoDeRelatorio().equalsIgnoreCase(EnumFormatos.TABELA.getFormato()))
				jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATEND_PERIODO_TABLE.getUrl());
			else if(Janela.getTipoDeRelatorio().equalsIgnoreCase(EnumFormatos.GRAFICO.getFormato()))
				jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATEND_PERIODO_GRAFICO.getUrl());
			else 
				jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATEND_PERIODO_LISTA.getUrl());

			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(atos));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}

	}
	public void showRelatorioQuantAtendimentoAFI(
			ArrayList<AtendimentoAnoDTO> atendimentosPeriodo,
			Map<String, Object> parametros) {
		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATEND_PERIODO_AFI_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(atendimentosPeriodo));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
	}
	public void showRelatorioQuantidadeAtndGrupo(
			ArrayList<TrabalhadorDTO> atendenteTOs, Map<String, Object> parametros) {
		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATEND_GRUPO_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(atendenteTOs));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
	}
	public void showRelatorioQuantidadeFaltasGrupo(
			ArrayList<FrequenciaGrupoDTO> frequenciaGrupoTO,
			Map<String, Object> parametros) {

		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_FALTAS_GRUPO_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(frequenciaGrupoTO));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
		
	}
	public void showRelatorioQuantidadeFreqAFI(
			ArrayList<Frequencia> frequencias, Map<String, Object> parametros) {

		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_FALTAS_AFI_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(frequencias));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
		
	}
	public void showRelatorioTipoAtende(
			ArrayList<TipoAtendedimentoDTO> tipoAtendimentoDTO,
			Map<String, Object> parametros) {
		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_TIPOATENDE_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(tipoAtendimentoDTO));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
	}
	public void showRelatorioQuantAtendimentoAtendido(
			ArrayList<AtendimentoDetalheDTO> atendidoAtendDTOs,
			Map<String, Object> parametros) {
		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.QUANT_ATEND_PERIODO_ATENDIDO_LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(atendidoAtendDTOs));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}		
	}
	public void showRelatorioAtendimentoDetalhe(
			ArrayList<AtendimentoDetalheDTO> atendimentoDTOs,
			Map<String, Object> parametros) {
		JasperReport jasper = null;
		try{
			jasper = JasperCompileManager.compileReport(EnumRelatorio.DETALHE_ATEND_PERIODO__LISTA.getUrl());
			JasperPrint print = JasperFillManager.fillReport(jasper,parametros,
					new JRBeanCollectionDataSource(atendimentoDTOs));
			viewer = new JRViewer(print);
			scrollPane.setViewportView(viewer);			
		}catch(Exception ex){
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}				
	}


}
